package com.pos.app.comm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils
{
	// ERROR //
	// public static String convertFiletoString(String paramString)
	// {
	// byte[] bytes = null;
	// try
	// {
	// // String str = "";
	// ByteArrayOutputStream bStream = null;
	// // Bitmap bitmap = BeanUtils.getFileImge(paramString);
	// if (bitmap != null)
	// {
	// bStream = new ByteArrayOutputStream();
	// bitmap.compress(CompressFormat.PNG, 100, bStream);
	// bytes = bStream.toByteArray();
	// // str = bStream.toString() ;
	// bStream.close();
	// bitmap.recycle();
	// }
	// }
	// catch (UnsupportedEncodingException e)
	// {
	// e.printStackTrace();
	// }
	// catch (IOException e)
	// {
	// e.printStackTrace();
	// }
	// return Base64.encode(bytes);
	// }

	public static void deleteFiles(String paramString)
	{
		File localFile = new File(paramString);
		if (localFile.exists())
			localFile.delete();
	}

	public static String generateId()
	{
		return DateUtil.stringFormat(new Date(), "%1$tY%1$tm%1$td%1$tH%1$tM%1$tS") + UUID.randomUUID().toString().replace("-", "");
	}

	public static String getFileFormatter(String paramString)
	{
		String str = "";
		if (!isEmpty(paramString))
		{
			int i = paramString.lastIndexOf(".");
			if (i > 0)
				str = paramString.substring(i);
		}
		return str;
	}

	public static int getNumberCount(String paramString)
	{
		int i = 0;
		if (!isEmpty(paramString))
		{
			for (int j = 0; j < paramString.length(); j++)
			{
				char c = paramString.charAt(j);
				if (String.valueOf(c).matches("[0-9]"))
					i++;
				if (String.valueOf(c).matches("[a-zA-Z]"))
					i++;
			}
			// i /= 2;
		}
		return i;
	}

	// public static String[] getPrinlines(String paramString, int paramInt)
	// {
	// String[] arrayOfString = new String[2];
	// int i =
	// Integer.valueOf(Configuration.getString(R.string.emop_print_line_charts_size)).intValue();
	// StringBuffer localStringBuffer = new StringBuffer();
	// if (!isEmpty(paramString))
	// {
	// for (int k = 0; k < paramInt; k++)
	// {
	// String str1 = "";
	// int m = i * (k + 1);
	// if (m > paramString.length())
	// {
	// m = paramString.length();
	// }
	// str1 = paramString.substring(k * i, m);
	// localStringBuffer.append(str1);
	// }
	// arrayOfString[0] = localStringBuffer.toString();
	// String str2 = "";
	// if (localStringBuffer.toString().length() >= paramString.length())
	// {
	// str2 = paramString.replace(localStringBuffer.toString(), "");
	// }
	// arrayOfString[1] = str2;
	// }
	// return arrayOfString;
	// }
	//
	// public static int getPrintLines(String paramString)
	// {
	// int i =
	// Integer.valueOf(Configuration.getString(R.string.emop_print_line_charts_size)).intValue();
	// int j = 0;
	// if (!isEmpty(paramString))
	// {
	// j = paramString.length() / i;
	// if (j * i < paramString.length())
	// j++;
	// }
	// return j;
	// }

	public static int getPrintLines(String paramString, int paramInt)
	{
		int i = 0;
		if (!isEmpty(paramString))
		{
			i = paramString.length() / paramInt;
			if (i * paramInt < paramString.length())
				i++;
		}
		return i;
	}

	public static boolean isEmpty(String paramString)
	{
		boolean bool = true;
		if ((paramString != null) && (!"".equals(paramString.trim())))
		{
			bool = false;
		}
		return bool;
	}

	public static double parseDouble(String paramString)
	{
		double d1 = 0;
		double d2 = 0;
		if (!isEmpty(paramString))
		{
			String str = trim(paramString);
			try
			{
				double d4 = Double.parseDouble(paramString);
				d2 = d4;
			}
			catch (NumberFormatException localNumberFormatException1)
			{
			}
			try
			{
				Matcher localMatcher = Pattern.compile("[\\d]+[.]?[\\d]*").matcher(str);
				if (localMatcher.find())
				{
					double d3 = Double.parseDouble(localMatcher.group());
					d1 = d3;
				}
				d2 = d1;
			}
			catch (NumberFormatException localNumberFormatException2)
			{
				d1 = 0;
			}
		}
		else
		{
			d2 = d1;
		}
		return d2;
	}

	public static String parseFileName(String paramString)
	{
		String str = paramString;
		if (!isEmpty(paramString))
		{
			String[] arrayOfString = paramString.replace(File.separator, "/").split("/");
			if ((arrayOfString != null) && (arrayOfString.length > 0))
				str = arrayOfString[(-1 + arrayOfString.length)];
		}
		return str;
	}

	public static String parseFileSingleName(String paramString)
	{
		return parseFileName(paramString).replace(getFileFormatter(paramString), "");
	}

	public static int parseInteger(String paramString)
	{
		int i = 0;
		String str = trim(paramString);
		try
		{
			Matcher matcher = Pattern.compile("[\\d]*").matcher(str);
			if (matcher.find())
			{
				i = Integer.parseInt(matcher.group());
			}
		}
		catch (NumberFormatException e)
		{
			i = 0;
		}
		return i;
	}

	public static long parseLong(String paramString)
	{
		long l1 = 0L;
		String str = trim(paramString);
		try
		{
			Matcher localMatcher = Pattern.compile("[\\d]*").matcher(str);
			if (localMatcher.find())
			{
				long l2 = Long.parseLong(localMatcher.group());
				l1 = l2;
			}
		}
		catch (NumberFormatException localNumberFormatException)
		{
			l1 = 0;
		}
		return l1;
	}

	public static String printLineString(String paramString, Integer paramInteger)
	{
		return paramString;
	}

	// public static String toHexString(byte[] paramArrayOfByte)
	// {
	// return Base64.encode(paramArrayOfByte);
	// }

	public static final String trim(String paramString)
	{
		String str = "";
		if (paramString != null)
		{
			str = paramString.trim();
		}
		return str;
	}

	public static void createFile(FileInputStream fileInputStream, String string)
	{
		File file = new File(string);
		if (!file.exists())
			try
			{
				file.createNewFile();
			}
			catch (IOException e1)
			{
				e1.printStackTrace();
			}
		InputStreamReader reader = new InputStreamReader(fileInputStream);
		BufferedReader buff = new BufferedReader(reader);
		FileOutputStream outputStream = null;
		OutputStreamWriter osw = null;
		PrintWriter pw = null;
		try
		{
			outputStream = new FileOutputStream(file);
			osw = new OutputStreamWriter(outputStream);
			pw = new PrintWriter(osw);
			String inLine = null;
			while ((inLine = buff.readLine()) != null)
			{
				pw.write(inLine);
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				buff.close();
				reader.close();
				pw.flush();
				pw.close();
				osw.close();
				outputStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}

		}
	}

	public static String equals(String str)
	{
		if (str.equals("Y"))
		{
			str = "是";
		}
		else if (str.equals("N"))
		{
			str = "否";
		}
		else
		{
			str = "否";
		}
		return str;
	}

	public static String getFileNameFromUrl(String url, String name)
	{

		// 名字不能只用这个
		// 通过 ‘？’ 和 ‘/’ 判断文件名
		String extName = "";
		String filename = "";
		if (url.indexOf("url=") != -1)
		{
			filename = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.'));
			extName = url.substring(url.lastIndexOf('.') + 1, url.length());
		}
		else
		{
			filename = url.substring(url.lastIndexOf("fileid=") + 7);// 取得文件ID
			extName = name.substring(name.lastIndexOf('.') + 1);// 取得文件后缀
		}
		filename = hashKeyForDisk(filename) + "." + extName;
		return filename;
	}

	/**
	 * 一个散列方法,改变一个字符串(如URL)到一个散列适合使用作为一个磁盘文件名。
	 */
	public static String hashKeyForDisk(String key)
	{
		String cacheKey;
		try
		{
			final MessageDigest mDigest = MessageDigest.getInstance("MD5");
			mDigest.update(key.getBytes());
			cacheKey = bytesToHexString(mDigest.digest());
		}
		catch (NoSuchAlgorithmException e)
		{
			cacheKey = String.valueOf(key.hashCode());
		}
		return cacheKey;
	}

	private static String bytesToHexString(byte[] bytes)
	{
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < bytes.length; i++)
		{
			String hex = Integer.toHexString(0xFF & bytes[i]);
			if (hex.length() == 1)
			{
				sb.append('0');
			}
			sb.append(hex);
		}
		return sb.toString();
	}

	public static boolean checkEndsWithInStringArray(String checkItsEnd, String[] fileEndings)
	{
		for (String aEnd : fileEndings)
		{
			if (checkItsEnd.endsWith(aEnd))
				return true;
		}
		return false;
	}

	//把NULL替换成空字符串
	public static String getString(String str)
	{
		try
		{
			if (str==null || str.startsWith("null") || str.endsWith("null"))
			{
				str = "";
			}
		}
		catch (Exception e)
		{
			str = "";
		}
		return str;
	}
	
	/**
	 * 请选择
	 */
	final static String PLEASE_SELECT = "请选择...";

	public static boolean empty(Object o)
	{
		return o == null || "".equals(o.toString().trim()) || "null".equalsIgnoreCase(o.toString().trim()) || "undefined".equalsIgnoreCase(o.toString().trim()) || PLEASE_SELECT.equals(o.toString().trim());
	}
	
	public static boolean num(Object o)
	{
		int n = 0;
		try
		{
			n = Integer.parseInt(o.toString().trim());
		}
		catch (NumberFormatException e)
		{
			e.printStackTrace();
		}
		if (n > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public static boolean decimal(Object o)
	{
		double n = 0;
		try
		{
			n = Double.parseDouble(o.toString().trim());
		}
		catch (NumberFormatException e)
		{
			e.printStackTrace();
		}
		if (n > 0.0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}